﻿using System;
using GDIT.Base.PageBaseHelper;
using GDIT.BLL;
using GDIT.Model;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxEditors;
using DevExpress.Xpo;

namespace GDIT.page.manage.notice
{
    public partial class list : System.Web.UI.Page
    {
        
        private BBulletinArticle bulletin = null;
        private BUsers user = null;
        private BCollegeInformationArticle college = null;
        private string type = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(Session["UserName"].ToString()))
                {
                    PageBase.MessageBoxShowAndGo("检查到你还没有登录，请先登录，谢谢！", "~/page/manage/login.aspx", Page);
                    return;
                }
                type = Request.QueryString["type"].ToString();
                if (!IsPostBack)
                {
                    databind();
                    BindConboBox();
                }
            }
            catch (Exception ex)
            {
                PageBase.MessageBoxShow("操作失败，原因：" + ex.Message.ToString(), Page);
            }
        }

        private void BindConboBox()
        {//在页面加载时，给combox列赋值,这里的workgroupID是在ASPxGridview中的Combox列绑定的字段
            if (type == "bulletin")
            {
                (ASPxGridView1.Columns["Publisher.OID"] as GridViewDataComboBoxColumn).PropertiesComboBox.ValueType = typeof(int);

                user = new BUsers();
                XPCollection xp = user.GetAllUser();
                foreach (Mtb_Users u in xp)
                {
                    int id = u.OID;
                    string v = u.UserName;
                    (ASPxGridView1.Columns["Publisher.OID"] as GridViewDataComboBoxColumn).PropertiesComboBox.Items.Add(new ListEditItem(v, id));

                }
            }
            else if (type == "information")
            {
                (ASPxGridView1.Columns["Publisher.OID"] as GridViewDataComboBoxColumn).PropertiesComboBox.ValueType = typeof(int);

                user = new BUsers();
                XPCollection xp = user.GetAllUser();
                foreach (Mtb_Users u in xp)
                {
                    int id = u.OID;
                    string v = u.UserName;
                    (ASPxGridView1.Columns["Publisher.OID"] as GridViewDataComboBoxColumn).PropertiesComboBox.Items.Add(new ListEditItem(v, id));

                }
            }
        }

        protected void ASPxGridView1_PageIndexChanged(object sender, EventArgs e)
        {
            databind();//只需重新绑定数据即可实现上下翻页
        }

        //绑定数据
        private void databind()
        {
            if (type == "bulletin")
            {
                bulletin = new BBulletinArticle();
                ASPxGridView1.KeyFieldName = "OID";//指定主键。直接更新数据和子表绑定需要用到
                ASPxGridView1.DataSource = bulletin.SelectBulletinByAdmin();
                ASPxGridView1.DataBind();
            }
            else if (type == "information")
            {
                college = new BCollegeInformationArticle();
                ASPxGridView1.KeyFieldName = "OID";//指定主键。直接更新数据和子表绑定需要用到
                ASPxGridView1.DataSource = college.SelectAllColegeByAdmin();
                ASPxGridView1.DataBind();
            }
        }

        protected void ASPxGridView1_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
        {
            e.Cancel = true;//否则，只有刷新页面才能看到删除后的结果
            try
            {
                if (type == "bulletin")
                {
                    int id = Convert.ToInt32(e.Keys[0]);//获取ID
                    bulletin = new BBulletinArticle();
                    bool result = bulletin.DeleteBulletinArticle(id);//从数据库删除记录
                    if (!result)
                    {
                        databind();//数据表绑定
                        PageBase.MessageBoxShow("抱歉，删除失败！", Page);
                    }
                    else
                    {
                        databind();//数据表绑定
                        PageBase.MessageBoxShow("恭喜, 删除成功！", Page);
                    }
                }
                else if (type == "information")
                {
                    int id = Convert.ToInt32(e.Keys[0]);//获取ID
                    college = new BCollegeInformationArticle();
                    bool result = college.DeleteCollegeInformationArticle(id);//从数据库删除记录
                    if (!result)
                    {
                        databind();//数据表绑定
                        PageBase.MessageBoxShow("抱歉，删除失败！", Page);
                    }
                    else
                    {
                        databind();//数据表绑定
                        PageBase.MessageBoxShow("恭喜, 删除成功！", Page);
                    }
                }
            }
            catch (Exception ex)
            {
                PageBase.MessageBoxShow("操作失败，原因：" + ex.Message.ToString(), Page);
            }
        }
    }
}